<?php

namespace app\index\service;

use think\facade\Cache;
use think\facade\Session;

class WebService extends BaseService
{
    /**
     * 网页授权登录获取的access_token到期，刷新
     * @author  messha1r
     * @date    2025-09-10
     * @param  string $openid      
     * @return  string
     */
    public function getWebAccessionTokenFromRefresh($openid, $app_id)
    {
        $refresh_token = Cache::get('refresh_token_' . $openid);
        if (empty($refresh_token)) {
            return redirect('/index/WeiXin/wxLogin');
        } else {
            $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';
            $url .= '?appid=' . $app_id;
            $url .= '&grant_type=refresh_token&refresh_token=' . $refresh_token;
            $data = file_get_contents($url);
            $res = json_decode($data, true);
            if (!isset($res['errcode'])) {
                $web_access_token = $res['access_token'];
                Session::set('web_access_token', $web_access_token);
                Cache::set('refresh_token_' . $res['openid'], $res['refresh_token'], 3600 * 24 * 30);
                return $web_access_token;
            } else {
                echo '发生错误' . $res['errmsg'];
                return '';
            }
        }
    }

    /**
     * 获取网页授权登录页的accss_token
     * @author  messha1r
     * @date    2025-09-10
     * @param  array $input 微信返回的数据
     * @param        
     * @return       
     */
    public function getWebAccessToken($input)
    {
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
            $url .= '?appid=' . $this->appId . '&secret=' . $this->secret;
            $url .= '&code=' . $input['code'];
            $url .= '&grant_type=authorization_code';
            $data = file_get_contents($url);
            // {
            //     "access_token":"ACCESS_TOKEN",
            //     "expires_in":7200,
            //     "refresh_token":"REFRESH_TOKEN",
            //     "openid":"OPENID",
            //     "scope":"snsapi_userinfo"
            // }
            $res = json_decode($data, true);
            Session::set('web_access_token', $res['access_token']);
            Cache::set('refresh_token_' . $res['openid'], $res['refresh_token'], 3600 * 24 * 30);
            return $res;
    }

    /**
     * 获取用户信息
     * @author  messha1r
     * @date    2025-09-10
     * @param   string $web_access_token    网页授权登录的token
     * @param   string $openid    
     * @return  array     
     */
    public function getUserInfo($web_access_token, $openid): array
    {
        if (empty($web_access_token) || empty($openid)) {
            return [];
        }
        $url = 'https://api.weixin.qq.com/sns/userinfo';
        $url .= '?access_token=' . $web_access_token;
        $url .= '&openid=' . $openid;
        $url .= '&lang=zh_CN';

        $data = file_get_contents($url);
        $res = json_decode($data, true);
        // ['openid'=>'asdgadsgds','nickname'=>'asdgadsg','sex'=>0,'language'=>'','city'=>'asdg',
        // 'province'=>'asdg','country'=>'asdga','headimgurl'=>?'https://asdgasdg','privilege'=>'asdgads'];
        return $res;
    }
}
